package weaver.jobs.zhlx.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.zhlx.duss.utils.ZhlxJobUtil;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-12-06
 * @Description: 定时调用SAP供应商主数据接口同步供应商主数据到建模表单
 * @ModifyLog:
 **/

public class WeaSyncGysBySapCron extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            ZhlxJobUtil jobUtil = new ZhlxJobUtil();
            String tablename = "uf_sapgyszsj";
            int formmodeid = jobUtil.getFormmodeid(tablename);
            JSONObject dataJson = new JSONObject();
            dataJson.put("BUKRS", "");
            dataJson.put("LIFNR", "");
            JSONObject resultJson = jobUtil.handleOKHttpRequest("FI034", dataJson);
            String code = Util.null2String(resultJson.getString("code"));
            baseBean.writeLog(">>>>>WeaSyncGysBySapCron>>>>>code=" + code);
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String msgText = outputJson.getString("MSG_TEXT");
                baseBean.writeLog(">>>>>WeaSyncGysBySapCron>>>>>msgType=" + msgType + ",msgText=" + msgText);
                if ("S".equals(msgType) && "调用成功".equals(msgText)) {
                    String dataReturnStr = outputJson.getString("DATA");
                    JSONObject dataReturnJson = JSONObject.parseObject(dataReturnStr);
                    String returnData = dataReturnJson.getString("RETURN");
                    JSONArray returnList = JSONArray.parseArray(returnData);
                    for (int i = 0; i < returnList.size(); i++) {
                        JSONObject paramJson = new JSONObject();
                        JSONObject returnJson = returnList.getJSONObject(i);
                        String returnHeaderStr = returnJson.getString("HEADER");
                        JSONObject returnHeaderJson = JSONObject.parseObject(returnHeaderStr);
                        paramJson.put("ywhbfz", returnHeaderJson.getString("KTOKK"));//业务伙伴分组
                        String gysbm = returnHeaderJson.getString("LIFNR");//供应商编号
                        paramJson.put("gysbm", gysbm);
                        String gysmc1 = returnHeaderJson.getString("NAME1");//供应商名称1
                        String gysmc2 = returnHeaderJson.getString("NAME2");//供应商名称2
                        paramJson.put("gysmc1", gysmc1);
                        paramJson.put("gysmc2", gysmc2);
                        paramJson.put("gysqc", gysmc1 + gysmc2);
                        paramJson.put("gj", returnHeaderJson.getString("LAND1"));//国家
                        paramJson.put("dq", returnHeaderJson.getString("ORT02"));//地区
                        paramJson.put("cs", returnHeaderJson.getString("ORT01"));//城市
                        paramJson.put("yzbm", returnHeaderJson.getString("PSTLZ"));//邮政编码
                        String jd1 =Util.null2String(returnHeaderJson.getString("STREET"));
                        String jd2 =Util.null2String(returnHeaderJson.getString("STR_SUPPL1"));
                        String jd3 =Util.null2String(returnHeaderJson.getString("STR_SUPPL2"));
                        String jd4 =Util.null2String(returnHeaderJson.getString("STR_SUPPL3"));
                        String jd5 =Util.null2String(returnHeaderJson.getString("LOCATION"));
                        String jd = jd1+jd2+jd3+jd4+jd5;//街道
                        paramJson.put("jd1",jd1.replaceAll("'","''"));
                        paramJson.put("jd2",jd2.replaceAll("'","''"));
                        paramJson.put("jd3",jd3.replaceAll("'","''"));
                        paramJson.put("jd4",jd4.replaceAll("'","''"));
                        paramJson.put("jd5",jd5.replaceAll("'","''"));
                        paramJson.put("jd", jd.replaceAll("'","''"));
                        paramJson.put("yydm", returnHeaderJson.getString("SPRAS"));//语言代码
                        paramJson.put("dh", returnHeaderJson.getString("TELF1"));//电话
                        paramJson.put("yddh", returnHeaderJson.getString("TELF2"));//移动电话
                        paramJson.put("cz", returnHeaderJson.getString("TELFX"));//传真
                        paramJson.put("dzyjdz", returnHeaderJson.getString("SMTP_ADDR"));//电子邮件地址
                        paramJson.put("wybsbh", returnHeaderJson.getString("IDNUMBER"));//唯一标识编号
                        paramJson.put("wybslxdx", returnHeaderJson.getString("IDTYPE"));//唯一标识类型
                        paramJson.put("myhbbh", returnHeaderJson.getString("VBUND"));//贸易伙伴编号
                        paramJson.put("djsygsdmjz", returnHeaderJson.getString("SPERR"));//冻结所有公司代码
                        int mainid = jobUtil.getMainId(tablename, "gysbm", gysbm);
                        if (mainid <= 0) {
                            int dataid = jobUtil.insertModelData(formmodeid, paramJson);
                            String bankStr = returnHeaderJson.getString("BANK");
                            JSONArray bankList = JSONArray.parseArray(bankStr);
                            if (!bankList.isEmpty()) {
                                for (int k = 0; k < bankList.size(); k++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("yxgj", bankList.getJSONObject(k).getString("BANKS"));//银行国家
                                    String yxzhdm = bankList.getJSONObject(k).getString("BANKL");//银行账号代码
                                    paramsJson.put("yxzhdm", yxzhdm);
                                    paramsJson.put("khyx", jobUtil.getKhyh(yxzhdm));
                                    paramsJson.put("yxzh", bankList.getJSONObject(k).getString("BANKN"));//银行账户
                                    paramsJson.put("ckxj", bankList.getJSONObject(k).getString("BKREF"));//参考细节
                                    paramsJson.put("swiftcode", bankList.getJSONObject(k).getString("SWIFT"));//SWIFT Code
                                    paramsJson.put("kzm", bankList.getJSONObject(k).getString("BKONT"));//控制码
                                    paramsJson.put("gjyxzh", bankList.getJSONObject(k).getString("IBAN"));//国际银行账号
                                    paramsJson.put("zhcyr", bankList.getJSONObject(k).getString("KOINH"));//账户持有人
                                    paramsJson.put("yxqksrq", bankList.getJSONObject(k).getString("BK_VALID_FROM"));//有效期开始日期
                                    paramsJson.put("yxqjsrq", bankList.getJSONObject(k).getString("BK_VALID_TO"));//有效期结束日期
                                    paramsJson.put("yxbz", bankList.getJSONObject(k).getString("ACCNAME"));//银行币种
                                    paramsJson.put("yxmc", bankList.getJSONObject(k).getString("BANKA"));//银行名称
                                    paramsJson.put("mainid", dataid);
                                    jobUtil.insertDetailData(tablename + "_dt1", paramsJson);
                                }

                            }

                            String orgStr = returnHeaderJson.getString("ORG");
                            JSONArray orgList = JSONArray.parseArray(orgStr);
                            if (!orgList.isEmpty()) {
                                for (int m = 0; m < orgList.size(); m++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("cgzz", orgList.getJSONObject(m).getString("EKORG"));//采购组织
                                    paramsJson.put("hbm", orgList.getJSONObject(m).getString("WAERS"));//货币码
                                    paramsJson.put("fktjdm", orgList.getJSONObject(m).getString("ZTERM"));//付款条件代码
                                    paramsJson.put("xsry", orgList.getJSONObject(m).getString("VERKF"));//销售人员
                                    paramsJson.put("gysdhhm", orgList.getJSONObject(m).getString("TELF1"));//供应商电话号码
                                    paramsJson.put("gjmytk", orgList.getJSONObject(m).getString("INCO1"));//国际贸易条款
                                    paramsJson.put("gjmytkbf2", orgList.getJSONObject(m).getString("INCO2"));//国际贸易条款部分2
                                    paramsJson.put("dj", orgList.getJSONObject(m).getString("SPERM"));//冻结
                                    paramsJson.put("mainid", dataid);
                                    jobUtil.insertDetailData(tablename + "_dt2", paramsJson);
                                }
                            }

                            String bukrsStr = returnHeaderJson.getString("BUKRS");
                            JSONArray bukrsList = JSONArray.parseArray(bukrsStr);
                            if (!bukrsList.isEmpty()) {
                                for (int j = 0; j < bukrsList.size(); j++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("gsdm", bukrsList.getJSONObject(j).getString("BUKRS"));//公司代码
                                    paramsJson.put("tykm", bukrsList.getJSONObject(j).getString("AKONT"));//统御科目
                                    paramsJson.put("pxm", bukrsList.getJSONObject(j).getString("ZUAWA"));//排序码
                                    paramsJson.put("xqdbh", bukrsList.getJSONObject(j).getString("ALTKN"));//先前的编号
                                    paramsJson.put("fktj", bukrsList.getJSONObject(j).getString("ZTERM"));//付款条件
                                    paramsJson.put("rcz", bukrsList.getJSONObject(j).getString("TOGRU"));//容差组
                                    paramsJson.put("jccffp", bukrsList.getJSONObject(j).getString("REPRF"));//检查重复发票
                                    paramsJson.put("khh", bukrsList.getJSONObject(j).getString("HBKID"));//开户行
                                    paramsJson.put("fkfs", bukrsList.getJSONObject(j).getString("ZWELS"));//付款方式
                                    paramsJson.put("djsygsdmjz", bukrsList.getJSONObject(j).getString("SPERR"));//对公司代码过帐冻结
                                    paramsJson.put("mainid", dataid);
                                    jobUtil.insertDetailData(tablename + "_dt3", paramsJson);
                                }
                            }
                        } else {
                            jobUtil.updateModelData(tablename, mainid, paramJson);
                            String bankStr = returnHeaderJson.getString("BANK");
                            JSONArray bankList = JSONArray.parseArray(bankStr);
                            if (!bankList.isEmpty()) {
                                jobUtil.deleteDetailData(tablename + "_dt1", mainid);
                                for (int k = 0; k < bankList.size(); k++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("yxgj", bankList.getJSONObject(k).getString("BANKS"));
                                    String yxzhdm = bankList.getJSONObject(k).getString("BANKL");
                                    paramsJson.put("yxzhdm", yxzhdm);
                                    paramsJson.put("khyx", jobUtil.getKhyh(yxzhdm));
                                    paramsJson.put("yxzh", bankList.getJSONObject(k).getString("BANKN"));
                                    paramsJson.put("ckxj", bankList.getJSONObject(k).getString("BKREF"));
                                    paramsJson.put("swiftcode", bankList.getJSONObject(k).getString("SWIFT"));
                                    paramsJson.put("kzm", bankList.getJSONObject(k).getString("BKONT"));
                                    paramsJson.put("gjyxzh", bankList.getJSONObject(k).getString("IBAN"));
                                    paramsJson.put("zhcyr", bankList.getJSONObject(k).getString("KOINH"));
                                    paramsJson.put("yxqksrq", bankList.getJSONObject(k).getString("BK_VALID_FROM"));
                                    paramsJson.put("yxqjsrq", bankList.getJSONObject(k).getString("BK_VALID_TO"));
                                    paramsJson.put("yxbz", bankList.getJSONObject(k).getString("ACCNAME"));//供应商简称2
                                    paramsJson.put("mainid", mainid);
                                    jobUtil.insertDetailData(tablename + "_dt1", paramsJson);
                                }

                            }

                            String orgStr = returnHeaderJson.getString("ORG");
                            JSONArray orgList = JSONArray.parseArray(orgStr);
                            if (!orgList.isEmpty()) {
                                jobUtil.deleteDetailData(tablename + "_dt2", mainid);
                                for (int m = 0; m < orgList.size(); m++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("cgzz", orgList.getJSONObject(m).getString("EKORG"));
                                    paramsJson.put("hbm", orgList.getJSONObject(m).getString("WAERS"));
                                    paramsJson.put("fktjdm", orgList.getJSONObject(m).getString("ZTERM"));
                                    paramsJson.put("gysdhhm", orgList.getJSONObject(m).getString("TELF1"));
                                    paramsJson.put("gjmytk", orgList.getJSONObject(m).getString("INCO1"));
                                    paramsJson.put("gjmytkbf2", orgList.getJSONObject(m).getString("INCO2"));
                                    paramsJson.put("mainid", mainid);
                                    jobUtil.insertDetailData(tablename + "_dt2", paramsJson);
                                }
                            }

                            String bukrsStr = returnHeaderJson.getString("BUKRS");
                            JSONArray bukrsList = JSONArray.parseArray(bukrsStr);
                            if (!bukrsList.isEmpty()) {
                                jobUtil.deleteDetailData(tablename + "_dt3", mainid);
                                for (int j = 0; j < bukrsList.size(); j++) {
                                    JSONObject paramsJson = new JSONObject();
                                    paramsJson.put("gsdm", bukrsList.getJSONObject(j).getString("BUKRS"));
                                    paramsJson.put("tykm", bukrsList.getJSONObject(j).getString("AKONT"));
                                    paramsJson.put("pxm", bukrsList.getJSONObject(j).getString("ZUAWA"));
                                    paramsJson.put("xqdbh", bukrsList.getJSONObject(j).getString("ALTKN"));
                                    paramsJson.put("fktj", bukrsList.getJSONObject(j).getString("ZTERM"));
                                    paramsJson.put("rcz", bukrsList.getJSONObject(j).getString("TOGRU"));
                                    paramsJson.put("jccffp", bukrsList.getJSONObject(j).getString("REPRF"));
                                    paramsJson.put("khx", bukrsList.getJSONObject(j).getString("HBKID"));
                                    paramsJson.put("fkfs", bukrsList.getJSONObject(j).getString("ZWELS"));
                                    paramsJson.put("djsygsdmjz", bukrsList.getJSONObject(j).getString("SPERR"));
                                    paramsJson.put("mainid", mainid);
                                    jobUtil.insertDetailData(tablename + "_dt3", paramsJson);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            baseBean.writeLog(">>>>>WeaSyncGysBySapCron>>>>>Exception=" + e);
        }

    }


}

